package com.jinyijia.admin.interfaces;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinyijia.admin.config.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestOperations;

import java.util.Collections;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * Created by yangrd on 2018/3/11
 * 产品接口
 **/
@RestController
@RequestMapping(Constant.Path.PRODUCT)
public class ProductController {

    @Autowired
    private RestOperations restOperations;

    @Value("${jinYiJia.api.products}")
    private String apiPath;

    @Value("${jinYiJia.api.categories}")
    private String categoriesApiPath;

    @Value("${jinYiJia.api.brands}")
    private String brandsApiPath;

    @PostMapping
    public ResponseEntity save(@RequestBody JSONObject jsonObject) {
        return restOperations.postForEntity(apiPath, jsonObject, JSONObject.class);
    }

    @PutMapping("/{id}/enable")
    public ResponseEntity changEnable(@PathVariable Long id, @RequestParam(defaultValue = "false") Boolean enable) {
        restOperations.put(apiPath + "/{1}/enable?enable={2}", null, id, enable);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable Long id) {
        return restOperations.getForEntity(apiPath + "/{1}", JSONObject.class, id);
    }

    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable Long id, @RequestBody JSONObject jsonObject) {
        restOperations.put(apiPath + "/{1}", jsonObject, id);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity findAll(@RequestParam(required = false) Long categoryId,
                                  @RequestParam(required = false) Long brandId,
                                  @RequestParam(required = false) Boolean enable,
                                  @RequestParam(defaultValue = "0") Long page,
                                  @RequestParam(defaultValue = "10") Long size) {
        StringBuilder url = new StringBuilder(apiPath).append("?page=").append(page).append("&size=").append(size);
        if (Objects.nonNull(categoryId)) {
            url.append("&categoryId=").append(categoryId);
        }
        if (Objects.nonNull(brandId)) {
            url.append("&brandId=").append(brandId);
        }
        if (Objects.nonNull(enable)) {
            url.append("&enable=").append(enable);
        }
        JSONObject jsonObject = restOperations.getForObject(url.toString(), JSONObject.class);
        convert(jsonObject);
        return ResponseEntity.ok(jsonObject);
    }


    private void convert(JSONObject jsonObject) {
        JSONArray jsonArray = jsonObject.getJSONArray("content");
        jsonObject.put("content", jsonArray.stream().map(o -> {
            JSONObject product = ((JSONObject) o);
            Long brandId = product.getLong("brandId");
            // TODO: 2018/3/13 此处应添加缓存会更好 
            //TODO getBrandName
            String brandName = restOperations.getForObject(brandsApiPath + "/{1}", JSONObject.class, brandId).getString("name");
            product.put("brandName", brandName);
            Long categoryId = product.getLong("categoryId");
            //TODO getCategoryName
            String categoryName = restOperations.getForObject(categoriesApiPath + "/{1}/parents-name", String.class, categoryId);
            product.put("categoryName", categoryName);
            return product;
        }).collect(Collectors.toList()));
    }
}
